package cn.cocho.android.mc.comp.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.cocho.android.mc.R;
import cn.cocho.android.mc.base.ResourcesUtils;

/**
 * 自定义对话框
 * 
 * @author hsx
 * @time 2013-5-7下午02:29:07
 */
public class ChoseDialog extends AlertDialog {

	TextView titleView;
	TextView contentView;
	Button okBtn;
	Button cancelBtn;
	TextView interval;
	View view;

	public ChoseDialog(Context context) {
		super(context);
		initComponent();
		setPositiveButton(ResourcesUtils.getString(R.string.ok));
		setNegativeButton(ResourcesUtils.getString(R.string.cancel));
	}

	private void initComponent() {
		show();// 需放在setContentView前，不然会出错
		setContentView(R.layout.chose_dialog);
		titleView = (TextView) findViewById(R.id.chose_dialog_title);
		contentView = (TextView) findViewById(R.id.chose_dialog_content);
		okBtn = (Button) findViewById(R.id.chose_dialog_ok_btn);
		cancelBtn = (Button) findViewById(R.id.chose_dialog_cancel_btn);
		interval = (TextView) findViewById(R.id.chose_dialog_interval);
	}

	@Override
	public void setTitle(CharSequence title) {
		titleView.setText(title);
		super.setTitle(title);
	}

	@Override
	public void setMessage(CharSequence message) {
		contentView.setText(message);
		super.setMessage(message);
	}

	public void setPositiveButton(CharSequence text, View.OnClickListener listener) {
		okBtn.setText(text);
		okBtn.setOnClickListener(listener);
	}

	public void setPositiveButton(CharSequence text) {
		okBtn.setText(text);
		okBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				cancel();
				dismiss();
			}
		});
	}

	public void setNegativeButton(CharSequence text, View.OnClickListener listener) {
		cancelBtn.setText(text);
		if (listener == null) {
			cancelBtn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					cancel();
					dismiss();
				}
			});
		} else {
			cancelBtn.setOnClickListener(listener);
		}
	}

	public void setNegativeButton(CharSequence text) {
		cancelBtn.setText(text);
		cancelBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				cancel();
				dismiss();
			}
		});
	}

	@Override
	public void onWindowAttributesChanged(LayoutParams params) {
		super.onWindowAttributesChanged(params);
	}

	public void setOneBtn(boolean flag) {
		if (flag) {
			interval.setVisibility(View.GONE);
			cancelBtn.setVisibility(View.GONE);
			okBtn.setLayoutParams(new LinearLayout.LayoutParams(ResourcesUtils.getDimen(R.dimen.mc_dp_100), ResourcesUtils
					.getDimen(R.dimen.mc_dp_35)));
		} else {
			interval.setVisibility(View.VISIBLE);
			cancelBtn.setVisibility(View.VISIBLE);
		}
	}

}
